var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[],
    page: 1,
    img_url: c.img_url,
    share_point: 0,
    share_start_content: "",
    share_end_content: ""
  },
  onLoad: function (options) {
    this.getShareInfo();
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;

    var data = {
      url: "/zfshop/Goodsagent/lists",
      page: that.data.page,
      limit: 10,
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        if (data.length > 0) {
          that.setData({
            page: that.data.page + 1
          });
        }
        for (var index in data) {
          data[index].pic = c.img_url + data[index].pic;
        }
        that.setData({
          goodsList: that.data.goodsList.concat(data),
        });
      } else {

      }
      wx.stopPullDownRefresh();
    });
  },
  onShow:function(){
    
  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    this.onLoad();
  },
  ///下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      goodsList: [],
      page: 1,
    });
    this.onLoad();
  },
  delagent:function(e){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          let goods_agent_id = e.currentTarget.dataset.id;
          var token = wx.getStorageSync("userinfo").wap_token;
          var data = {
            url: "/zfshop/goodsagent/del",
            id: goods_agent_id,
            token: token,
          };
          utils.request(data).then(function (res) {
            if (res.code == 0) {
              utils.errMsg("删除成功");
              setTimeout(function(){
                that.onPullDownRefresh();
              }, 1000);
            }
          });
        }
      }
    })
  },
  //分享
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === "button") {
      var id = res.target.dataset.id;
      var name = res.target.dataset.name;
      var pic = res.target.dataset.pic;
      var description = res.target.dataset.description;
      var price = res.target.dataset.price;
      return {
        title: that.data.share_start_content + name + " ￥" + price + that.data.share_end_content,
        path: '/pages/shop/goodsdetail/index?id=' + id + "&invitecode=" + wx.getStorageSync("userinfo").invitecode,
        imageUrl: pic,
        success: function (res) {
          wx.getSystemInfo({
            success: function (d) {
              var token = wx.getStorageSync("userinfo").wap_token;
              var data = {
                url: "/wsgoods/usershare/add",
                token: token,
                type: 0,
                id: id
              };
              utils.request(data).then(function (res) {
                if (res.code == 0) {

                }
              });
            }
          })
        },
      }
    }
  },
  //分享信息
  getShareInfo: function () {
    var that = this;
    var data = {
      url: "/zfcom/com/deploy",
      uuid: "share_point,share_start_content,share_end_content",
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        that.setData({
          share_point: res.data.share_point,
          share_start_content: res.data.share_start_content,
          share_end_content: res.data.share_end_content,
        });
      }
    });
  },
})